package pers.muci.spring.lesson001.demo1;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;
import java.util.List;

/**
 * ClassName: GetAllBeanTest
 * Description:
 * date: 2022/1/21 11:06
 *
 * @author muci
 * @since JDK 1.8
 */
public class GetAllBeanTest {
    public static void main(String[] args) {
        // 1.bean 配置文件位置
        String path = "classpath:/lesson001/demo1/bean01.xml";
        //2.创建 ClassPathXmlApplicationContext 容器，指定该容器需要加载的bean配置文件
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
        //3.定义所有的 bean 名称，，获取指定bean的别名
        List<String> beanNames = Arrays.asList("demo1", "demo2", "demo3");
        for (String beanName : beanNames) {
            //4.获取bean别名
            String[] aliases = applicationContext.getAliases(beanName);
            System.out.println(String.format("beanName:%s,别名:[%s]", beanName, String.join(",", aliases)));
        }

        //5.获取容器中所有bena的别名,getBeanDefinitionNames()返回容器在中的所有定义的名称
        System.out.println("获取容器中所有bean的别名");

        for (String definitionName : applicationContext.getBeanDefinitionNames()) {
            String[] aliases = applicationContext.getAliases(definitionName);
            System.out.println(String.format("beanName:%s,别名:[%s]", definitionName, String.join(",", aliases)));
        }


    }
}
